<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Support\Facades\Request;
use HiCommon\Service\PublicService;

class PublicController extends BaseController
{
    /**
     * 普通上传图片
     */
    public function uploadImg()
    {
        $res = upload_single_file($_FILES["file"]);

        echo json_encode(['code' => $res['state'], 'url' => $res['data']['url'], 'src' => $res['data']['src']]);

        //return upload_single_file($_FILES["file"]);
        /*if ($_FILES["file"]["error"] == 0) {
            $fileUrl = 'upload/imgs';
            $dir = iconv("UTF-8", "GBK", $fileUrl);
            if (!file_exists($dir)) {
                mkdir($dir, 777, true);
            }
            $suffix = '.' . pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
            $picUrl = $fileUrl . '/' . self::GetRandStr() . $suffix;
            move_uploaded_file($_FILES["file"]["tmp_name"], $picUrl);
            echo json_encode(['code' => '0', 'url' => $picUrl,'full_url'=>1]);
        } else {

            echo json_encode(['code' => '1', 'url' => '']);
        }*/
    }

    /**
     * 普通上传文件
     */
    public function uploadFile()
    {
        $arr_option = [
            'project' => 'apk',
            'size' => 100,
        ];
        $res = upload_one_file($_FILES["file"], $arr_option);

        echo json_encode(['code' => $res['state'], 'url' => $res['data']['url'], 'src' => $res['data']['src']]);
    }

    //随机数
    public function GetRandStr()
    {
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str) - 1;
        $randstr = '';
        for ($i = 0; $i < 6; $i++) {
            $num = mt_rand(0, $len);
            $randstr .= $str[$num];
        }
        return time() . $randstr;
    }

    /**
     *省市区三级联动(获取省列表)
     */
    public function getProvinceList()
    {
        $list = PublicService::getProvinceList();

        dd(json_encode($list));
    }

    /**
     *省市区三级联动(获取城市列表)
     */
    public function getList()
    {
        $pid = Request::all();
        return PublicService::getCityList($pid);
    }

    /**
     * 物流追踪
     * 2019 11 15
     */
    public function logisticsTracking()
    {
        $order_id = intval(Request::get('id', 0));
        $type = intval(Request::get('type', 0));

        $res = PublicService::logistics_tracking($order_id, $type);
        echo_error($res);

        return view('admin::store_order.delivery', $res['data']);
    }

    /**
     *  设置敏感字
     *  hinq 2020 611
     *
     **/
    public function sensitivity_index(){
//        $content = \Redis::get('sensitivity');
        $arr_list = PublicService::get_all();
        $content = $arr_list['data']->content;
        return view("admin::public.sensitivity" , ['content'=>$content]);
    }


    /**
     *  保存、更新敏感词库
     *  hinq 2020 6 11
     *
     **/
    public function sensitivity_update(){
        $arr_input = Request::all();
        return PublicService::sensitivity_update( (array) $arr_input );
    }

    /**
     *  检索敏感词是否已经添加
     *  hinq 2020 6 12
     *
     **/
    public function sensitivity_search(){
        $title = (string) Request::get('title');
        return PublicService::sensitivity_search( (string) $title );
    }

}
